<template>
  <div class="register">
    <div class="register_form">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item label="" prop="name"
          ><el-input
            type="text"
            autocomplete="off"
            v-model="ruleForm.name"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入用户名"
          ></el-input
        ></el-form-item>
        <el-form-item label="" prop="pass"
          ><el-input
            type="password"
            autocomplete="off"
            v-model="ruleForm.pass"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input
        ></el-form-item>
        <el-form-item label="" prop="checkPass"
          ><el-input
            type="password"
            autocomplete="off"
            v-model="ruleForm.checkPass"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input
        ></el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >注册</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
 
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      activeName: "second",
      ruleForm: {
        name: "",
        pass: "",
        checkPass: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入您的名称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            type: "success",
            message: "注册成功",
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
.register {
  width: 100%;
  height: 100vh;
  background-size: 100% 100%;
  background-image: url("../../public/index_pic/图层1.png");
  background-position: center center;
  overflow: auto;
  position: relative;
  .register_form {
    width: 400px;
    height: 360px;
    position: absolute;
    left: 78%;
    top: 50%;
    margin-left: -200px;
    margin-top: -150px;
    padding: 10px;
    background: rgb(191, 207, 215);
    border-radius: 10px;
    box-shadow: 0 0 10px #ddd;
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
  p {
    font-size: 24px;
    text-align: center;
    font-weight: 600;
  }
}
</style>